//
//  Global.swift
//  U17
//
//  Created by charles on 2017/10/24.
//  Copyright © 2017年 None. All rights reserved.
//

import Foundation
import UIKit
import Kingfisher
import SnapKit
import MJRefresh

extension UIColor {
    class var background: UIColor {
        return UIColor(r: 250, g: 250, b: 250)
    }
    class var tip: UIColor {
        return UIColor(r: 190, g: 190, b: 190, a: 0.8)
    }
    class var theme: UIColor {
        return UIColor(r: 29, g: 221, b: 43)
    }
    class var colBlack: UIColor {
         return UIColor(r: 16, g: 16, b: 16)
    }
    class var colOrange: UIColor {
        return UIColor(r: 235, g: 139, b: 45)
    }
    class var colHui: UIColor {
        return UIColor(r: 193, g: 197, b: 198)
    }
    class var colHuiwhite: UIColor {
        return UIColor(r: 240, g: 240, b: 240)
    }
    class var colblueHui: UIColor {
        return UIColor(r: 246, g: 249, b: 255)
    }
    class var colGray: UIColor {
        return UIColor(r: 149, g: 153, b: 155)
    }
    class var colGreen: UIColor {
        return UIColor(r: 18, g: 189, b: 1)
    }
}
// 屏幕尺寸
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
// 常用尺寸
let size5 = 5
let size10 = 10
let size15 = 15
let size20 = 20
let size25 = 25
let size30 = 30
let size40 = 40
let size50 = 50
let size60 = 60
let PAGESIZE = 20

extension String {
    static let token = "token"
    static let userId = "userId"
    static let roleType = "roleType"
    static let authStatus = "authStatus"
     static let phone = "phone"
static let isPwd = "isPwd"
    
    static let rongyunToken = "rongyunToken"
    static let rongyunAppKey = "rongyunAppKey"
    static let rongyunNickName = "rongyunNickName"
    static let rongyunAvatar = "rongyunAvatar"
    static let rongyunUserId = "rongyunUserId"
    static let selectIndex = "selectIndex"
    static let cateId = "cateId"
} 

extension NSNotification.Name {
    static let USexTypeDidChange = NSNotification.Name("USexTypeDidChange")
}


var topVC: UIViewController? {
    var resultVC: UIViewController?
    resultVC = _topVC(UIApplication.shared.keyWindow?.rootViewController)
    while resultVC?.presentedViewController != nil {
        resultVC = _topVC(resultVC?.presentedViewController)
    }
    return resultVC
}

var isIphoneX: Bool {
    return UI_USER_INTERFACE_IDIOM() == .phone
        && (max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) == 812
        || max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) == 896)
}

private  func _topVC(_ vc: UIViewController?) -> UIViewController? {
    if vc is UINavigationController {
        return _topVC((vc as? UINavigationController)?.topViewController)
    } else if vc is UITabBarController {
        return _topVC((vc as? UITabBarController)?.selectedViewController)
    } else {
        return vc
    }
}

//MARK: print
func uLog<T>(_ message: T, file: String = #file, function: String = #function, lineNumber: Int = #line) {
    #if DEBUG
        let fileName = (file as NSString).lastPathComponent
        print("[\(fileName):funciton:\(function):line:\(lineNumber)]- \(message)")
    #endif
}

//MARK: Kingfisher
extension Kingfisher where Base: ImageView {
    @discardableResult
    public func setImage(urlString: String?, placeholder: Placeholder? = UIImage(named: "normal_placeholder_h")) -> RetrieveImageTask {
        return setImage(with: URL(string: urlString ?? ""),
                        placeholder: placeholder,
                        options:[.transition(.fade(0.5))])
    }
}

extension Kingfisher where Base: UIButton {
    @discardableResult
    public func setImage(urlString: String?, for state: UIControl.State, placeholder: UIImage? = UIImage(named: "normal_placeholder_h")) -> RetrieveImageTask {
        return setImage(with: URL(string: urlString ?? ""),
                        for: state,
                        placeholder: placeholder,
                        options: [.transition(.fade(0.5))])
        
    }
}
//MARK: SnapKit
extension ConstraintView {
    
    var usnp: ConstraintBasicAttributesDSL {
        if #available(iOS 11.0, *) {
            return self.safeAreaLayoutGuide.snp
        } else {
            return self.snp
        }
    }
}

extension UICollectionView {
    
    func reloadData(animation: Bool = true) {
        if animation {
            reloadData()
        } else {
            UIView .performWithoutAnimation {
                reloadData()
            }
        }
    }
}

extension UIView {
    //返回该view所在VC
    func firstViewController() -> UIViewController? {
        for view in sequence(first: self.superview, next: { $0?.superview }) {
            if let responder = view?.next {
                if responder.isKind(of: UIViewController.self){
                    return responder as? UIViewController
                }
            }
        }
        return nil
    }
    
    //返回该view所在的父view
    func superView<T: UIView>(of: T.Type) -> T? {
        for view in sequence(first: self.superview, next: { $0?.superview }) {
            if let father = view as? T {
                return father
            }
        }
        return nil
    }
    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        // also  set(newValue)
        set {
            layer.cornerRadius = newValue
        }
    }
    
    //sb设置边框宽度
    @IBInspectable var border: CGFloat {
        get {
            return layer.borderWidth
        }
        // also  set(newValue)
        set {
            layer.borderWidth = newValue
        }
    }
    
    @IBInspectable var boderColor:UIColor {
        set{
            self.layer.borderColor = newValue.cgColor
        }
        get{
            return UIColor(cgColor: self.layer.borderColor!)
        }
    }
    
    @IBInspectable var shadowColor:UIColor{
        set{
            self.layer.shadowColor = newValue.cgColor
        }
        get{
            return UIColor(cgColor: self.layer.shadowColor!)
        }
    }
    
    @IBInspectable var shadowOffset:CGSize{
        set{
            self.layer.shadowOffset = newValue
        }
        get{
            return self.layer.shadowOffset
        }
    }
    
    @IBInspectable var shadowOpacity:Float{
        set{
            self.layer.shadowOpacity = newValue
        }
        get{
            return self.layer.shadowOpacity
        }
    }
}

//MARK: swizzledMethod
extension NSObject {
    
    static func swizzleMethod(_ cls: AnyClass, originalSelector: Selector, swizzleSelector: Selector){
        
        let originalMethod = class_getInstanceMethod(cls, originalSelector)!
        let swizzledMethod = class_getInstanceMethod(cls, swizzleSelector)!
        let didAddMethod = class_addMethod(cls,
                                           originalSelector,
                                           method_getImplementation(swizzledMethod),
                                           method_getTypeEncoding(swizzledMethod))
        if didAddMethod {
            class_replaceMethod(cls,
                                swizzleSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UIApplication {
    
    private static let u_initialize: Void = {
        UINavigationController.u_initialize
        if #available(iOS 11.0, *) {
            UINavigationBar.u_initialize
        }
    }()
    
    open override var next: UIResponder? {
        UIApplication.u_initialize
        return super.next
    }
}

extension UserDefaults {
    
    func setUser(userId: Int, roleType: Int,authStatus:Int,token:String) {
        
        UserDefaults.standard.set(userId, forKey: String.userId)
        UserDefaults.standard.set(roleType, forKey: String.roleType)
        UserDefaults.standard.set(authStatus, forKey: String.authStatus)
        UserDefaults.standard.set(token, forKey: String.token)
    }
    
}
